package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    //物资来源分页列表展示
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum,
                                               Integer pageSize,
                                               String name,
                                               String address,
                                               String contact) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Supplier.class);
        Example.Criteria criteria = o.createCriteria();
        o.setOrderByClause("sort asc");
        if (name !=null && !"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if (address !=null && !"".equals(address)){
            criteria.andLike("address","%"+address+"%");
        }
        if (contact !=null && !"".equals(contact)){
            criteria.andLike("contact","%"+contact+"%");
        }
        List<SupplierVO> supplierVOList = supplierMapper.selectByExample(o).stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier,supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());

        PageInfo<SupplierVO> info = new PageInfo<>(supplierVOList);
        return new PageVO<>(info.getTotal(),supplierVOList);
    }

    //添加物资来源
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insertSelective(supplier);
    }

    //编辑物资来源
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }

    //修改物资来源
    @Override
    public void update(Long id,SupplierVO supplierVO) {
        Supplier supplierById = supplierMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(supplierVO,supplierById);
        supplierById.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplierById);
    }

    //删除物资来源
    @Override
    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }
}
